package nix.mall.item.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 静态页面生成Controller
 */
@Controller
public class HtmlGenController {

	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	
	@RequestMapping("/test/genHtml")
	@ResponseBody
	public String genHtml() throws Exception {
		// 生成静态页面
		Configuration configuration = freeMarkerConfigurer.getConfiguration();
		Template template = configuration.getTemplate("hello.ftl");
		Map data = new HashMap<>();
		data.put("hello", "Hello, FreeMaker! " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		Writer out = new FileWriter(new File("D:/tmp/hello.txt"));
		template.process(data, out);
		out.close();
		// 返回结果
		return "OK";
		// 测试访问：http://localhost:8096/test/genHtml.html
	}
}
